<?php
namespace app\api\controller;

class Hook
{
    const PASSWORD = '123456';

    public function index()
    {
        config('app_debug', false);
        config('app_trace', false);
        $post = file_get_contents('php://input');
        $data = json_decode($post, true);
        $branch = input('branch', 'master');
        if (JSON_ERROR_NONE !== json_last_error()) {
            return '';
        }
        if ($data['password'] !== self::PASSWORD) {
            return '';
        }
//        $commitid = $data['commits']['id'];
        $cmd = 'cd '.ROOT_PATH;
        $res1 = shell_exec($cmd);
        $cmd = 'git pull origin '.$branch;
        $res2 = shell_exec($cmd);
        error_log(var_export([$res1, $res2], true),  3, __DIR__.'/hook.log');
//        $cmd = 'chown www.www -R '.ROOT_PATH;
//        shell_exec($cmd);
        return 'success';

//        $cmd = 'git reset --hard '.$commitid;
//        shell_exec($cmd);
    }
}